<?php

namespace app\common\model;

use app\common\library\MemberVo;
use app\common\library\TencentMap;
use think\Env;
use think\Exception;
use think\Model;

/**
 * 模型
 */
class UsedCar extends Model
{

    // 表名
    protected $name = 'used_car';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    protected $deleteTime = false;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    // 追加属性
    protected $append = [

    ];

    public function member()
    {
        return $this->belongsTo('Member', 'member_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function models()
    {
        return $this->belongsTo('CarModels', 'models_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function series()
    {
        return $this->belongsTo('CarSeries', 'series_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function brand()
    {
        return $this->belongsTo('CarBrands', 'brand_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function shop()
    {
        return $this->belongsTo('shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function cartype()
    {
        return $this->belongsTo('EnergyType', 'car_type', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function level()
    {
        return $this->belongsTo('car_level', 'level', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function discharge()
    {
        return $this->belongsTo('discharge', 'discharge', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function gearbox()
    {
        return $this->belongsTo('gearbox', 'gearbox', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function swept()
    {
        return $this->belongsTo('SweptVolume', 'swept_volume', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getCoverImgAttr($value) {
        return $value ? Env::get('app.app_host').$value : '';
    }

    public function getDownPaymentAttr($value) {
        return $value ?? '';
    }

//    public function getPriceAttr($value) {
//        return $value ? $value.'万元' : '';
//    }
//
//    public function getMileageAttr($value) {
//        return $value ? $value.'万公里' : '';
//    }

    public function setCoverImgAttr($value)
    {
        if (!empty($value)){
            $arr = parse_url($value);
            return $arr['path'];
        }
        return '';
    }


    public static function getList($params)
    {
        if ($params['self'] == 1)
            $where['member_id'] = MemberVo::instance()->getMemberId();
        $list = self::field('cover_img,id,name,phone,car_age,mileage,price')->where($where)->order('id desc')->paginate(10);
        $list_arr = $list->toArray();
        return $list_arr;
    }


    /**
     * 二手车添加
     * @param $params
     * @return UsedCar
     */
    public static function addCar($params)
    {
        $arr = [
            'car_age'   =>$params['car_age'] ?? 0,
            'mileage'   =>$params['mileage'] ?? 0,
            'phone'   =>$params['phone'] ?? '',
            'price'   =>$params['price'] ?? 0,
            'detail'   =>$params['detail'] ?? '',
            'status'   =>0,
            'member_id'   =>$params['member_id'] ?? 0,
            'cover_img'   =>$params['cover_img'] ?? '',
            'province'   =>$params['province'] ?? 0,
            'city'   =>$params['city'] ?? 0,
            'districts'   =>$params['districts'] ?? '',
            'address'   =>$params['address'] ?? '',
            'brand_id'   =>$params['brand_id'] ?? 0,
            'series_id'   =>$params['series_id'] ?? 0,
            'models_id'   =>$params['models_id'] ?? 0,
            'car_type'   =>$params['car_type'] ?? 0,
            'level'   =>$params['level'] ?? 0,
            'discharge'   =>$params['discharge'] ?? 0,
            'gearbox'   =>$params['gearbox'] ?? 0,
            'swept_volume'   =>$params['swept_volume'] ?? 0,
            'insurance_expires'   =>$params['insurance_expires'] ?? '',
            'transfer_num'   =>$params['transfer_num'] ?? 0,
            'down_payment'   =>$params['down_payment'] ?? '',
            'type'   =>$params['type'] ?? 0,
            'shop_id'   =>$params['shop_id'] ?? 0,
            'licence_city'   =>$params['licence_city'] ?? '',
            'longitude'   =>$params['longitude'] ?? '',
            'latitude'   =>$params['latitude'] ?? '',
        ];
        return self::create($arr);
    }


    /**
     * 二手车更新
     * @param $params
     * @return UsedCar
     */
    public static function updateCar($params)
    {
        $arr = [
            'car_age'   =>$params['car_age'] ?? 0,
            'mileage'   =>$params['mileage'] ?? 0,
            'phone'   =>$params['phone'] ?? '',
            'price'   =>$params['price'] ?? 0,
            'detail'   =>$params['detail'] ?? '',
            'status'   =>0,
            'member_id'   =>$params['member_id'] ?? 0,
            'cover_img'   =>$params['cover_img'] ?? '',
            'province'   =>$params['province'] ?? 0,
            'city'   =>$params['city'] ?? 0,
            'districts'   =>$params['districts'] ?? '',
            'address'   =>$params['address'] ?? '',
            'brand_id'   =>$params['brand_id'] ?? 0,
            'series_id'   =>$params['series_id'] ?? 0,
            'models_id'   =>$params['models_id'] ?? 0,
            'car_type'   =>$params['car_type'] ?? 0,
            'level'   =>$params['level'] ?? 0,
            'discharge'   =>$params['discharge'] ?? 0,
            'gearbox'   =>$params['gearbox'] ?? 0,
            'swept_volume'   =>$params['swept_volume'] ?? 0,
            'insurance_expires'   =>$params['insurance_expires'] ?? '',
            'transfer_num'   =>$params['transfer_num'] ?? 0,
            'down_payment'   =>$params['down_payment'] ?? '',
            'licence_city'   =>$params['licence_city'] ?? '',
            'longitude'   =>$params['longitude'] ?? '',
            'latitude'   =>$params['latitude'] ?? '',
            'preview_qrcode'   =>$params['preview_qrcode'] ?? '',
        ];
        return self::update($arr,['id'=>$params['id']]);
    }







}
